package com.xian.im.web.modules.group;

import com.xian.im.common.event.group.enums.GroupEventEnum;
import com.xian.im.common.event.group.model.GroupEvent;
import com.xian.im.common.event.system.EventBusUtil;
import com.xian.im.common.exception.PermissionException;
import com.xian.im.common.model.from.GroupFrom;
import com.xian.im.common.model.from.UserFrom;
import com.xian.im.common.model.vo.GroupVo;
import com.xian.im.common.model.vo.UserVo;
import com.xian.im.common.result.RestResult;
import com.xian.im.common.result.RestResultBuilder;
import com.xian.im.common.utils.sys.ThreadLocalUtil;
import com.xian.im.modules.avchat.manager.ImGroupManager;
import com.xian.im.modules.im.core.IGroupCoreService;
import com.xian.im.modules.im.core.IGroupUserCoreService;
import com.xian.im.modules.im.core.IUserCoreService;
import com.xian.im.modules.security.BaseClaims;
import com.xian.im.modules.security.TokenProvider;
import com.xian.im.modules.security.TokenUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Set;


/**
 * 
 *
 * @author xlr
 * @email 
 * @date 2021-01-18 19:11:56
 */
@RestController
@RequestMapping("im/group")
@Api(value = "群组操作API文档", tags = {"群组"})
public class GroupController {

    @Autowired
    private IGroupCoreService groupCoreService;
    @Autowired
    private IUserCoreService userCoreService;
    @Autowired
    private ImGroupManager groupManager;
    @Autowired
    private IGroupUserCoreService groupUserCoreService;

    @ApiOperation(value = "添加群组", notes = "添加群组")
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public RestResult<GroupVo> add(@RequestBody GroupFrom from){
        GroupVo group = groupCoreService.createGroup(from);
        GroupEvent event = new GroupEvent();
        event.setEventEnum(GroupEventEnum.CREATE);
        event.setGroupUniqueId(group.getGroupUniqueId());
        EventBusUtil.postGroupEvent(event);
        return RestResultBuilder.builder().success().data(group).build();
    }
    @ApiOperation(value = "修改群组信息", notes = "修改群组信息")
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public RestResult<GroupVo> update(@RequestBody GroupFrom from){
        GroupVo group = groupCoreService.updateGroup(from);
        return RestResultBuilder.builder().success().data(group).build();
    }
    @ApiOperation(value = "群组禁言", notes = "群组禁言")
    @RequestMapping(value = "/shutUp",method = RequestMethod.GET)
    public RestResult<Boolean> shutUp(@RequestParam("uniqueId") String uniqueId){
        Boolean group = groupCoreService.shutUp(uniqueId,getOrgId());
        return RestResultBuilder.builder().success().data(group).build();
    }
    @ApiOperation(value = "解除群组禁言", notes = "解除群组禁言")
    @RequestMapping(value = "/unmute",method = RequestMethod.GET)
    public RestResult<Boolean> unmute(@RequestParam("uniqueId") String uniqueId){
        Boolean group = groupCoreService.unmute(uniqueId,getOrgId());
        return RestResultBuilder.builder().success().data(group).build();
    }
    @ApiOperation(value = "查询群组是否禁言 true 是 false 否", notes = "查询群组是否禁言")
    @RequestMapping(value = "/isGroupMute",method = RequestMethod.GET)
    public RestResult<Boolean> isGroupMute(@RequestParam("uniqueId") String uniqueId){
        Boolean aBoolean = groupCoreService.groupMute(uniqueId,getOrgId());
        return RestResultBuilder.builder().success(aBoolean).data(Boolean.TRUE).build();
    }
    @ApiOperation(value = "删除群组", notes = "删除群组")
    @RequestMapping(value = "/del",method = RequestMethod.GET)
    public RestResult<Boolean> del(@RequestParam("uniqueId") String uniqueId){
        Boolean group = groupCoreService.delGroupByUniqId(uniqueId,getOrgId());
        GroupEvent event = new GroupEvent();
        event.setEventEnum(GroupEventEnum.DEL);
        event.setGroupUniqueId(uniqueId);
        EventBusUtil.postGroupEvent(event);
        return RestResultBuilder.builder().success().data(group).build();
    }
    @ApiOperation(value = "设置群管理", notes = "设置群管理")
    @RequestMapping(value = "/addGroupManager",method = RequestMethod.GET)
    public RestResult<Boolean> addGroupManager(@RequestParam("groupUniqueId") String groupUniqueId,@RequestParam("userAccount") String userAccount){
        String account = TokenUtil.getAccount();
        if(groupUserCoreService.exist(account,groupUniqueId)){
            Boolean group = groupUserCoreService.addManager(userAccount,groupUniqueId);
            return RestResultBuilder.builder().success().data(group).build();
        }else {
            throw new PermissionException("权限不足");
        }
    }
    @ApiOperation(value = "撤销群管理", notes = "撤销群管理")
    @RequestMapping(value = "/removeGroupManager",method = RequestMethod.GET)
    public RestResult<Boolean> removeGroupManager(@RequestParam("groupUniqueId") String groupUniqueId,@RequestParam("userAccount") String userAccount){
        String account = TokenUtil.getAccount();
        if(groupUserCoreService.exist(account,groupUniqueId)){
            Boolean group = groupUserCoreService.removeManager(userAccount,groupUniqueId);
            return RestResultBuilder.builder().success().data(group).build();
        }else {
            throw new PermissionException("权限不足");
        }
    }
    @ApiOperation(value = "查询是否是群管理 true 是", notes = "查询是否是群管理 true 是")
    @RequestMapping(value = "/existGroupManager",method = RequestMethod.GET)
    public RestResult<Boolean> existGroupManager(@RequestParam("groupUniqueId") String groupUniqueId,@RequestParam("userAccount") String userAccount){
        Boolean group = groupUserCoreService.exist(userAccount,groupUniqueId);
        return RestResultBuilder.builder().success().data(group).build();
    }
    @ApiOperation(value = "查询所有群管理", notes = "查询所有群管理")
    @RequestMapping(value = "/getGroupManagerInfos",method = RequestMethod.GET)
    public RestResult<Set<UserVo>> getGroupManagerInfos(@RequestParam("groupUniqueId") String groupUniqueId){
        Set<UserVo> userVos = groupUserCoreService.getUserInfoByGroupId(groupUniqueId);
        return RestResultBuilder.builder().success().data(userVos).build();
    }
    @ApiOperation(value = "查询所有群管理唯一账号", notes = "查询所有群管理唯一账号")
    @RequestMapping(value = "/getGroupManagers",method = RequestMethod.GET)
    public RestResult<Set<String>> getGroupManagerAccounts(@RequestParam("groupUniqueId") String groupUniqueId){
        Set<String> userVos = groupUserCoreService.getUserAccountByGroupId(groupUniqueId);
        return RestResultBuilder.builder().success().data(userVos).build();
    }
    @ApiOperation(value = "转移群主", notes = "转移群主")
    @RequestMapping(value = "/update/owner",method = RequestMethod.GET)
    public RestResult<Boolean> updateGroupOwner(@RequestParam("userAccount")String userAccount,@RequestParam("groupUniqueId") String groupUniqueId){
        Boolean group = groupCoreService.updateGroupOwner(userAccount,groupUniqueId);
        return RestResultBuilder.builder().success(group).data(Boolean.TRUE).build();
    }
    @ApiOperation(value = "踢出在线用户", notes = "踢出在线用户")
    @RequestMapping(value = "/kickOut",method = RequestMethod.POST)
    public RestResult<Boolean> kickOut(@RequestBody UserFrom from){
        if(from.getAccount().equals(TokenUtil.getAccount())){
            return RestResultBuilder.builder().failure().message("不能踢出自己").build();
        }
        Boolean aBoolean = userCoreService.kickOut(from);
        return RestResultBuilder.builder().success(aBoolean).data(Boolean.TRUE).build();
    }
    @ApiOperation(value = "成员禁言", notes = "成员禁言")
    @RequestMapping(value = "/mute",method = RequestMethod.POST)
    public RestResult<Boolean> mute(@RequestBody UserFrom from){
        if(from.getAccount().equals(TokenUtil.getAccount())){
            return RestResultBuilder.builder().failure().message("不能禁言自己").build();
        }
        userCoreService.mute(from);
        return RestResultBuilder.builder().success(Boolean.TRUE).data(Boolean.TRUE).build();
    }
    @ApiOperation(value = "解除禁言", notes = "解除禁言")
    @RequestMapping(value = "/removeMute",method = RequestMethod.POST)
    public RestResult<Boolean> removeMute(@RequestBody UserFrom from){
        userCoreService.removeMute(from);
        return RestResultBuilder.builder().success(Boolean.TRUE).data(Boolean.TRUE).build();
    }
    @ApiOperation(value = "查询用户是否禁言 true 是 false 否", notes = "查询用户是否禁言")
    @RequestMapping(value = "/isMute",method = RequestMethod.POST)
    public RestResult<Boolean> isMute(@RequestBody UserFrom from){
        Boolean aBoolean = userCoreService.isMute(from);
        return RestResultBuilder.builder().success(aBoolean).data(Boolean.TRUE).build();
    }
    @ApiOperation(value = "发送群组消息", notes = "发送群组消息")
    @RequestMapping(value = "/sendMessage",method = RequestMethod.GET)
    public RestResult<Boolean> sendMessage(@RequestParam("body")String body,@RequestParam("groupUniqueId")String groupUniqueId){
        groupManager.sendMessage(body,groupUniqueId);
        return RestResultBuilder.builder().success(true).data(Boolean.TRUE).build();
    }

    private Long getOrgId(){
        BaseClaims claims = ThreadLocalUtil.get(TokenProvider.USER_CLAIMS);
        return claims.getOrgId();
    }
}
